//
//  LGHelpDetailVideoController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/5/23.
//  视频

#import "LGHelpDetailVideoController.h"
#import <AVFoundation/AVFoundation.h>
#import "ZFPlayerController.h"
#import "ZFAVPlayerManager.h"
#import "ZFPlayerControlView.h"
@interface LGHelpDetailVideoController ()
/**   */
@property (nonatomic , strong)ZFPlayerController *player;
/**   */
@property (nonatomic , strong)ZFPlayerControlView *controlView;
/**   */
@property (nonatomic , strong)UIView *fatherView;
@end

@implementation LGHelpDetailVideoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.fatherView];
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    playerManager.shouldAutoPlay = NO;
    /// 播放器相关
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.fatherView];
    self.player.controlView = self.controlView;
    self.player.pauseWhenAppResignActive = YES;//设置退到后台继续播放
    self.player.pauseByEvent = YES;
    [self videoPlayerCallBack];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/help/helpdetail" parameters:@{@"help_id":self.helpId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.title = LGNSString(returnData[@"title"]);
            self.player.assetURL = [NSURL URLWithString:returnData[@"media_uri_view"]];
            [self.controlView showTitle:LGNSString(returnData[@"title"]) coverImage:nil fullScreenMode:ZFFullScreenModeAutomatic];
            [self.player.currentPlayerManager play];
        }
    } error:^(NSError *error) {
        
    }];
}

//视频播放回调
- (void)videoPlayerCallBack{
    __weak typeof(self) weakSelf = self;
    self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
//        AppDelegate *myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//        myDelegate.allowOrentitaionRotation = isFullScreen;
    };
    
    // 播放完成
    self.player.playerDidToEnd = ^(id  _Nonnull asset) {
        //回到半屏
        [weakSelf.player enterFullScreen:NO animated:YES];
        [weakSelf.player.currentPlayerManager seekToTime:0 completionHandler:^(BOOL finished) {
            [weakSelf.player.currentPlayerManager pause];
        }];
//        if (weakSelf.index<weakSelf.videoArray.count-1) {
//            weakSelf.index++;
//        }else{
//            //播放结束
//            [weakSelf.player stop];
//        }
    };
    //播放中
    self.player.playerPlayTimeChanged = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset, NSTimeInterval currentTime, NSTimeInterval duration) {
        //试看结束
//        NSInteger ptimes = [weakSelf.mModel.ptimes integerValue];
//        NSInteger times = [weakSelf.mModel.times integerValue];
//        if (times>ptimes && currentTime >= ptimes && [weakSelf.cModel.havebuy isEqualToString:@"N"] && [kIsFree isEqualToString:@"N"]&&![weakSelf.mModel.status isEqualToString:@"gy"]) {
//            if (ptimes == 0) {
//                [TooltipView showMessage:@"收费内容，请购买后观看" offset:0];
//            }else{
//                [TooltipView showMessage:@"试看结束，请付费解锁" offset:0];
//            }
//            [weakSelf.player setPauseByEvent:YES];
//        }
//        CGFloat percent = currentTime/duration;
//        if (percent>0.9) {
//            [weakSelf playEndRecord];
//        }
    };
}


-(void)dealloc{
    LGLogFunction;
    [self.player stopCurrentPlayingView];
}

#pragma mark
#pragma mark ====> 创建控件

//视频的占位View
-(UIView *)fatherView{
    if (!_fatherView) {
        _fatherView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_W/16.0*9.0)];
        _fatherView.backgroundColor = [UIColor blackColor];
    }
    return  _fatherView;
}

- (ZFPlayerControlView *)controlView{
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.5;
        _controlView.prepareShowLoading = YES;
        _controlView.prepareShowControlView = NO;
    }
    return _controlView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
